import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-04-13
 * Time:22:53
 */
public class Test4 {
    public int[] inventoryManagement(int[] stock, int cnt) {
        qsort(stock,0,stock.length-1,cnt);
        int[] ret=new int[cnt];
        for(int i=0;i<cnt;i++){
            ret[i]=stock[i];
        }
        return ret;
    }
    public void qsort(int[] stock,int l,int r,int cnt){
        if(l>=r){
            return;
        }
        //随机找基准元素+数组分三块
        int key=stock[new Random().nextInt(r-l+1)+l];
        int left=l-1,right=r+1,i=l;
        while(i<right){
            if(stock[i] < key){
                swap(stock,++left,i++);
            }else if(stock[i]==key){
                i++;
            }else{
                swap(stock,--right,i);
            }
        }
        //分类讨论
        int a=left-l+1,b=right-left-1;
        if(a>cnt){
            qsort(stock,l,left,cnt);
        }else if(a+b>=cnt){
            return;
        }else{
            qsort(stock,right,r,cnt-a-b);
        }
    }
    public void swap(int[] stock,int i,int j){
        int t=stock[i];
        stock[i]=stock[j];
        stock[j]=t;
    }
}
